//
//  BKMyOrderCheckLogisticsController.m
//
//
//  Created by mac on 16/12/15.
//
//

#import "BKMyOrderCheckLogisticsController.h"
#import "BKMyOrderLogistModel.h"
#import "BkMyOrderLogisticsTableViewCell.h"
#import "BKMyOrderLogisticsView.h"
#import "ExceptionView.h"


#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"

@interface BKMyOrderCheckLogisticsController ()
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSMutableArray *muArr;
@property (nonatomic, strong) NSString *str_company;
@property (nonatomic, strong) NSString *str_OrderNum;
@property (nonatomic, strong) UILabel *addLabel;
@property (nonatomic, strong) BKMyOrderLogisticsView *headerView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *heightArray;
@property (nonatomic, strong) NSMutableArray *companyArray;
@property (nonatomic, strong) NSMutableArray *logisticsNOArray;


@property (nonatomic, strong) ExceptionView *exceptionView;

@end

@implementation BKMyOrderCheckLogisticsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"物流信息";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;

    _heightArray = [NSMutableArray array];
    _companyArray = [NSMutableArray array];
    _logisticsNOArray = [NSMutableArray array];
    _dataArray = [NSMutableArray array];
    
    [self getData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updageLogicMsg) name:@"updageLogicMsg" object:nil];
}

-(void)getData{
    [BKIndicationView showInView:self.view];
    if (self.exceptionView) {
        [self.exceptionView dismiss];
        self.exceptionView = nil;
    }
    [BKMyOrderGetDataFromNetWork GetOrderLogisticsWithOrderno:self.orderNo Success:^(id obj) {
        [BKIndicationView dismiss];
        if ([obj isKindOfClass:[NSDictionary class]]) {
            BOOL value =[obj[@"success"] boolValue];
            if (value) {
                for (UIView *view in [self.view subviews])
                {
                    [view removeFromSuperview];
                } 
                NSDictionary *testDic = obj;
                NSMutableArray *msgArray = [NSMutableArray array];
                NSArray *getArray = testDic[@"data"];
                for (NSDictionary *dic in getArray) {
                    
                    [_companyArray addObject:dic[@"express_compnay"]];
                    [_logisticsNOArray addObject:dic[@"express_no"]];
                    NSArray *array = dic[@"logistics_data"];
                    for (NSDictionary *dict in array) {
                        
                       
                        
                        BKMyOrderLogistModel *logistModel = [[BKMyOrderLogistModel alloc]initWithDictionary:dict];
                        [msgArray addObject:logistModel];
                    }
                    [_dataArray addObject:[msgArray copy]];
                    [msgArray removeAllObjects];
                }
               
                [self logistics];
            }else{
                if (obj[@"msg"]) {
                    
                    
                    self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
                    ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeNoData handler:^(ExceptionViewAction *action) {
                        [self.exceptionView dismiss];
                        self.exceptionView = nil;
                        [self getData];
                    }];
                    
                    [self.exceptionView addAction:action];
                    [self.exceptionView showInView:self.view];
                    
                    
                    
                }
            }
            [self.tableView reloadData];
            
        }    } failure:^(NSError *error) {
            [BKIndicationView dismiss];
            


            
            ExceptionCodeType errorType;
            if (error.code == -1009) {
                errorType = ExceptionCodeTypeNoNetwork;
            } else if (error.code == -1001){
                errorType = ExceptionCodeTypeRequestFailed;
            }else if(error.code == 502 || error.code == 404){
                errorType =  ExceptionCodeTypeServerError;
            } else {
                errorType = ExceptionCodeTypeFailedToLoad;
            }
           
            
            
            self.exceptionView = [ExceptionView exceptionViewWithType:errorType];
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:errorType handler:^(ExceptionViewAction *action) {
                [self.exceptionView dismiss];
                self.exceptionView = nil;
                [self getData];
            }];
            
            [self.exceptionView addAction:action];
            [self.exceptionView showInView:self.view];
            
        }];
    
    
}

-(void)updageLogicMsg{
    if (_dataArray.count) {
        _muArr = _dataArray[_headerView.currentIndex - 99];
        [_tableView reloadData];
    }
    
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)logistics{
    
    _muArr = [_dataArray firstObject];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setTranslatesAutoresizingMaskIntoConstraints:NO];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
    
    _headerView = [BKMyOrderLogisticsView new];
    _headerView.orderLabel.text = [NSString stringWithFormat:@"%@", self.orderNo];
    _headerView.logisticsArray = _companyArray;
    _headerView.logisticsNumberArray = _logisticsNOArray;
    _headerView.companyLabel.text = [NSString stringWithFormat:@"%@",_headerView.logisticsArray[0]];
    _headerView.logisticsOrderNO.text = [NSString stringWithFormat:@"%@", _headerView.logisticsNumberArray[0]];
    
    _headerView.noticLabel.text = @"如果查询不到物流信息，可能是由于快递公司官网的查询系统延迟，请您稍后再进行物流查询。";
    _headerView.backgroundColor = [UIColor whiteColor];
    
    NSMutableArray *titleArray = [NSMutableArray array];
    for (NSInteger i = 0; i < _headerView.logisticsArray.count; i++) {
        NSString *title = [NSString stringWithFormat:@"包裹%ld", (long)i+1];
        [titleArray addObject:title];
    }
    
    
    _headerView.titles = titleArray;
//    _headerView.lineView.backgroundColor = RGB(0, 186, 156);
    _headerView.lineView.backgroundColor = kOrderBKGreen;
    
    UIView *headerView = [[UIView alloc]init];
    if (_dataArray.count == 1) {
        headerView.bounds = CGRectMake(0, 0, ScreenWidth, 270);
    }else{
        headerView.bounds = CGRectMake(0, 0, ScreenHeight, 306);
    }
    [headerView addSubview:_headerView];
    self.tableView.tableHeaderView = headerView;
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_tableView.mas_top);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(@306);
    }];
    
    if (_dataArray.count == 1) {
        
        _headerView.titleScrollView.hidden = YES;
        [_headerView.titleScrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@0);
        }];
        [_headerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@270);
        }];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _muArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BKMyOrderLogisticsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Logistics&%ld", (long)indexPath.row]];
    
    if (cell == nil) {
        cell = [[BKMyOrderLogisticsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Logistics&%ld", (long)indexPath.row]];
    }
    BKMyOrderLogistModel *logistic = _muArr[indexPath.row];
    
    if (indexPath.row == 0) {
//        cell.msgLabel.textColor = RGB(0, 186, 156);
//        cell.timeLabel.textColor = RGB(0, 186, 156);
        
        cell.msgLabel.textColor =  kOrderBKGreen;
        cell.timeLabel.textColor = kOrderBKGreen;
        
        
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(32.5, 0, 15, 15)];
        imageV.image = [UIImage imageNamed:@"物流到达"];
        [cell.contentView addSubview:imageV];
    }
    cell.userInteractionEnabled = NO;
    cell.selected = NO;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (indexPath.row == _muArr.count -1) {
        cell.lineView.hidden = YES;
    }
    cell.msgLabel.text = logistic.remark;
    cell.timeLabel.text = logistic.time;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BKMyOrderLogisticsTableViewCell *cell = (BKMyOrderLogisticsTableViewCell *)[self tableView:_tableView cellForRowAtIndexPath:indexPath];
    BKMyOrderLogistModel *logistic = _muArr[indexPath.row];//_muArr.count-indexPath.row-1
    [cell setIntroductionText:logistic.remark];
    return cell.frame.size.height;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




@end
